package org.devocean.groupbuying.web.interceptors;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.devocean.groupbuying.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MenuInterceptor extends HandlerInterceptorAdapter {

	private String defaultLocale;
	@Autowired
	private MenuService menuService;

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (request.getCookies() != null) {
			for (Cookie c : request.getCookies()) {
				if (c.getName().equalsIgnoreCase("clientLanguage")) {
					defaultLocale = c.getValue();
				}
			}

		}
		request.setAttribute("menus", menuService.getAllMenus(defaultLocale));
		super.postHandle(request, response, handler, modelAndView);
	}

	public String getDefaultLocale() {
		return defaultLocale;
	}

	public void setDefaultLocale(String defaultLocale) {
		this.defaultLocale = defaultLocale;
	}

}
